﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Library.Bll;
using Library.Common;

namespace HanhuaMicroCredit.Admin.Controllers
{
    public class BlackListController : Controller
    {
        //
        // GET: /BlackList/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult List()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Add(FormCollection fc)
        {
            Dictionary<string, object> d = new Dictionary<string, object>();
            foreach (string k in fc.Keys)
            {
                if (k.IndexOf("t0_") >= 0)
                {
                    d[k.Replace("t0_", "")] = fc[k];
                }
            }
            try
            {
                if (IsExist(d["Phone"].ToString()))
                {
                    d["isValid"] = 1;
                    CommonBLL.Update("blacklist", d, string.Format("phone='{0}'", d["Phone"].ToString()));
                }
                else
                {
                    CommonBLL.Add("BlackList", d);
                }
                //return Content("{\"statusCode\":\"200\",\"message\":\"添加成功\",\"navTabId\":\"navTab\",\"callbackType\":\"closeCurrent\"}");
                return Content(JUIResult.ResultUrl("/blacklist/list"));
            }
            catch (Exception e)
            {
                return Content(JUIResult.ResultMsg(e.Message));
            }
        }

        [HttpPost]
        public ActionResult GetList(string isValid, string phone, int pageindex, int pagesize, string status)
        {
            string table = "BlackList";
            string columns = "id, phone, createdate, case isvalid when 1 then '启用' when 0 then '禁用' end as status";
            string condition = "";

            if (!string.IsNullOrEmpty(isValid))
            {
                condition += string.Format("isvalid={0}", isValid);
            }
            if (!string.IsNullOrEmpty(phone))
            {
                condition += string.Format(" and phone like '%{0}%'", phone);
            }
            PagedInfo p = new PagedInfo(pageindex, pagesize);

            PagedResult<DataSet> pR = CommonBLL.GetPagedList(table, columns, "id desc", condition, p);
            return PartialView("~/Views/BlackList/List.ascx", pR);
        }

        public bool IsExist(string phone)
        {
            string sql = string.Format("select count(*) from blacklist where phone='{0}'", phone);
            int count = (int)CommonBLL.ExecuteSQL4Scalar(sql);
            if (count > 0)
                return true;
            return false;
        }
    }
}
